<?php

class CleanUpzHandler extends ScriptBase
{
    private $_redis;
    private $optime;

    /**
     * 清除过期的UPZ用户数据
     */
	public function logic()
	{
	    set_time_limit(0);
	    $this->optime = time() - 7*24*3600;
        
        // 清除UPZ过期数据
        $this->_redis = $this->redis('paiju_log');
        $this->_redis->select(5);
        
        $min = $this->mysql('game')->findRow('d_user',array(),array('order'=>'uid asc','fields'=>'uid'));
        $min = intval($min['uid']); 
        
        $max = $this->mysql('game')->findRow('d_user',array(),array('order'=>'uid desc','fields'=>'uid'));
        $max = intval($max['uid']);
        
        $total = 0;
        $j = 1;
        for ($i=$min;$i<=$max;$i++){
            if( $j % 1000 == 0){
                //file_put_contents('./upz.log', date('Y-m-d H:i:s')." ".($i-1000)." - {$i}".PHP_EOL, FILE_APPEND);
                usleep(100000);
            }
            $opkey = 'UPZ:'.$i;
            if($this->_redis->exists($opkey)){
                //echo $opkey."\n";
                $this->trim_data($opkey);
            }
            $j++;
        }
        $this->deinitRedis('paiju_log');
	}
    
	//截取数据
    private function trim_data($opkey){
        $i = 1;
        $step = 10;
        $index = 0;
        while (true) {
            $index = $step * $i;
            $center = $this->_redis->lindex($opkey,$index);
            if(empty($center)) break;
            $ctime = intval(substr($center, 0,10));
            if($ctime <= $this->optime){
                break ;
            }
            $i ++;
            //usleep(10000);
        }
        $this->_redis->ltrim($opkey,0,$index);
        usleep(10000);
    }
}